package com.dly.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dly.blog.domain.GptSessionContent;
import com.dly.blog.domain.ro.ChatGptRO;
import com.dly.blog.mapper.GptSessionContentMapper;
import com.dly.blog.service.GptSessionContentService;
import com.dly.blog.utile.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class GptSessionContentImpl extends ServiceImpl<GptSessionContentMapper, GptSessionContent> implements GptSessionContentService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveContent(String sessionId, ChatGptRO chatGptRO) {
        this.saveBatch(chatGptRO.cover(sessionId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveContent(String userId, String sessionId, ChatGptRO chatGptRO) {
        this.saveBatch(chatGptRO.cover(sessionId, userId));
    }

    @Override
    public Integer getUseNum() {
        LambdaQueryWrapper<GptSessionContent> user = new LambdaQueryWrapper<GptSessionContent>()
                .eq(GptSessionContent::getCreateUser, SecurityUtils.getUserId())
                .eq(GptSessionContent::getRole, "user");
        return (int)this.count(user);
    }
}
